<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/3/11
 * Time: 15:41
 */

namespace app\admin\controller;


use think\Db;

class Withdraw extends Base
{
    /**
     * 提现列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $phone = input('phone');
            $user_username = input('user_username');
            $user_real_name = input('user_real_name');
            $withdraw_status = input('withdraw_status');
            $where = [];
            if(isset($phone) && $phone != "")
            {
                $where['user_phone'] = ['like', "%" . $phone . "%"];
            }
            if(isset($user_username) && $user_username != "")
            {
                $where['user_username'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($user_real_name) && $user_real_name != "")
            {
                $where['withdraw_user_real_name'] = ['like', '%' . $user_real_name . '%'];
            }
            if(isset($withdraw_status) && $withdraw_status != "")
            {
                $where['withdraw_status'] = $withdraw_status;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_withdraw')
                ->join('up_u_user', 'user_id = withdraw_user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_withdraw')
                ->join('up_u_user', 'user_id = withdraw_user_id', 'left')
                ->where($where)
                ->field('withdraw_id, withdraw_money, withdraw_bank_code, withdraw_bank_name, 
                withdraw_user_real_name, withdraw_status, withdraw_time, user_phone, user_username, 
                withdraw_check_time')
                ->page($NowPage,$limits)
                ->order('withdraw_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 审核通过
     * @return \think\response\Json
     */
    public function allow(){
        $withdraw_id = input('id');
        $res = Db::table('up_withdraw')
            ->where('withdraw_id', $withdraw_id)
            ->update(['withdraw_status' => 2, 'withdraw_check_time' => date('Y-m-d H:i:s')]);
        if($res){
            return json(['code' => 200, 'msg' => '审核成功']);
        }else{
            return json(['code' => 100, 'msg' => '审核失败']);
        }
    }
    
    /**
     * 审核驳回
     * @return \think\response\Json
     */
    public function refuse(){
        $withdraw_id = input('id');
        $user = Db::table('up_withdraw')
            ->field('withdraw_user_id, withdraw_gold')
            ->where('withdraw_id', $withdraw_id)
            ->find();
        Db::startTrans();
        try{
            Db::table('up_withdraw')
                ->where('withdraw_id', $withdraw_id)
                ->update(['withdraw_status' => 3, 'withdraw_check_time' => date('Y-m-d H:i:s')]);
            Db::table('up_u_user')
                ->where('user_id', $user['withdraw_user_id'])
                ->setInc('user_balance', $user['withdraw_gold']);
            Db::commit();
            return json(['code' => 200, 'msg' => '审核成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '审核失败']);
        }
    }
}